#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
#include <QWindow>

struct MainWindowPrivate
{
    MainWindowPrivate():
        cmdProcess(nullptr),
        notepad(nullptr)
    {

    }
    QProcess *cmdProcess;
     QProcess *notepad;
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    _p(new MainWindowPrivate)
{
    ui->setupUi(this);
    _p->cmdProcess = new QProcess;
    _p->notepad = new QProcess;
    //connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(OnClickedExe()));

//    connect(_p->cmdProcess, &QProcess::readyRead, [=] () {
//        QTextCodec *codec = QTextCodec::codecForName("GBK");
//        QString dir = codec->toUnicode(_p->cmdProcess->readAll());
//        ui->textEdit->append(dir);
//    });
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(OnClickedExe_2()));

}

MainWindow::~MainWindow()
{
    delete ui;
    delete _p;
}

void MainWindow::OnClickedExe()
{

    QString arg = ui->lineEdit->text();
    QString program = "C:/Windows/System32/cmd.exe";
    QStringList arguments = arg.split(" ");
   // ui->textEdit->append("启动命令" + arg + "\n");
    _p->cmdProcess->start(program,arguments);
}

void MainWindow::OnClickedExe_2()
{
    QString cmd = "./notepad++.exe";
    _p->cmdProcess->setWorkingDirectory("E:\Software\Notepad++");
    _p->cmdProcess->start(cmd);

    if(!_p->cmdProcess->waitForFinished(2000))
       {
           qDebug()<<"--------";
       }
    WId wid = (WId)FindWindow(L"Notepad",nullptr);
    QWindow * window = QWindow::fromWinId(wid);
        if (window)
        {
            window->setFlags(window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //这边可以设置一下属性

            QWidget * widget = QWidget::createWindowContainer(window, ui->widget1, Qt::Widget);


            ui->verticalLayout->addWidget(widget);
        }
}
